package com.kgcx.system.controller;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.controller.BaseController;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.system.domain.SysRole;
import com.kgcx.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 何凯
 * @version 0.1
 * @description
 * @time 2017/12/13 17:07
 * @since 0.1
 */
@Api(description = "系统角色", tags = "系统角色接口")
@RestController
@RequestMapping("sys/v1/role")
public class SysRoleController extends BaseController{

    @Autowired
    private SysRoleService sysRoleService;

	@ApiOperation(value = "获取角色", httpMethod = "GET", notes = "获取角色及菜单接口")
	@GetMapping(value = "get")
	@ApiImplicitParams({
		 @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
	})
	public ReturnData<SysRole> getSysRole(@RequestParam Integer roleId) {
         SysRole sysRole=  sysRoleService.getSysRole(roleId);
		return new ReturnData<SysRole>(sysRole);
	}

	@ApiOperation(value = "获取所有角色", httpMethod = "GET", notes = "获取所有角色接口")
	@GetMapping(value = "getList")
	@ApiImplicitParams({
		 @ApiImplicitParam(name = "name", value = "角色名称", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
	})
	public ReturnData<List<SysRole>> getSysRoleList(@RequestParam(value = "name", required = false) String name) {
		 SysRole role = new SysRole();
         role.setName(name);
         List<SysRole> list=  sysRoleService.querySysRoleListByName(role);
		return new ReturnData<List<SysRole>>(list);
	}

	@ApiOperation(value = "获取角色列表", httpMethod = "POST", notes = "获取角色列表接口")
	@PostMapping(value = "queryWithPage")
	@ApiImplicitParams({
         @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
         @ApiImplicitParam(name = "pageSize", value = "页容量", required = true, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
		 @ApiImplicitParam(name = "name", value = "角色名称", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
		 @ApiImplicitParam(name = "isDefault", value = "是否默认角色", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
		 @ApiImplicitParam(name = "isEnabled", value = "是否启用", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
		 @ApiImplicitParam(name = "createId", value = "创建人", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
	})
	public ReturnData<List<SysRole>> querySysRoleWithPage(
	        @RequestParam(value = "pageNum", required = true) Integer pageNum,
			@RequestParam(value = "pageSize", required = true) Integer pageSize,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "isDefault", required = false) Integer isDefault,
			@RequestParam(value = "isEnabled", required = false) Integer isEnabled,
			@RequestParam(value = "createId", required = false) Integer createId) {
	    SysRole role = new SysRole();
            role.setName(name);
            role.setIsDefault(isDefault);
            role.setIsEnabled(isEnabled);
            role.setCreateId(createId);
        PageUtils.page(pageNum,pageSize);
	    List<SysRole> list = sysRoleService.querySysRoleList(role);
		return new ReturnData<>(list);
	}
	@ApiOperation(value = "添加系统角色", httpMethod = "POST", notes = "添加系统角色接口")
	@PostMapping(value = "save")
	public ReturnData<SysRole> addSysRole(@RequestBody SysRole sysRole){
	        sysRoleService.saveSysRoleMenu(sysRole);
		return new ReturnData<>();
	}

	@ApiOperation(value = "修改系统角色", httpMethod = "POST", notes = "修改系统角色接口")
	@PostMapping(value = "update")
    public ReturnData<?> updateSysRole(@RequestBody SysRole sysRole) {
            sysRoleService.updateSysRole(sysRole);
		return new ReturnData<>();
	}
	/**
	 * 根据roleId删除系统角色
	 * @return
	 */
	@ApiOperation(value = "删除系统角色", httpMethod = "DELETE", notes = "删除系统角色接口")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
	})
	@DeleteMapping("delete")
	public ReturnData<SysRole> deleteSysRole(@RequestParam Integer roleId) {
            sysRoleService.deleteSysRole(roleId);
		return new ReturnData<>();
	}
}
